package com.intelligentpension.kotlinbluetooth.utils
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Intent
import android.bluetooth.BluetoothDevice
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothHeadset
import android.bluetooth.BluetoothA2dp
import android.content.Context
import android.content.IntentFilter
import com.orhanobut.logger.Logger

/**
 * 监听蓝牙广播-各种状态
 */
class BtReceiver(cxt: Context, private val mListener: Listener) : BroadcastReceiver() {
    @SuppressLint("MissingPermission")
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action ?: return
//        Logger.e("===$action")
        val dev = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
//        if (dev != null) Logger.e("BluetoothDevice: " + dev.name + ", " + dev.address)
        when (action) {
            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)
                Logger.e("STATE: $state")
            }
            BluetoothAdapter.ACTION_DISCOVERY_STARTED -> {}
            BluetoothAdapter.ACTION_DISCOVERY_FINISHED -> {}
            BluetoothDevice.ACTION_FOUND -> {
                val rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MAX_VALUE)
                Logger.e("EXTRA_RSSI:$rssi")
                mListener.foundDev(dev)
            }
            BluetoothDevice.ACTION_PAIRING_REQUEST -> {}
            BluetoothDevice.ACTION_BOND_STATE_CHANGED ->
                Logger.e("BOND_STATE: " + intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, 0))
            BluetoothDevice.ACTION_ACL_CONNECTED -> {}
            BluetoothDevice.ACTION_ACL_DISCONNECTED -> {}
            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED ->
                Logger.e("CONN_STATE: " + intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0))
            BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED ->
                Logger.e("CONN_STATE: " + intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0))
            BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED ->
                Logger.e("CONN_STATE: " + intent.getIntExtra(BluetoothA2dp.EXTRA_STATE, 0))
        }
    }

    interface Listener {
        fun foundDev(dev: BluetoothDevice?)
    }

    init {
        val filter = IntentFilter()
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED) //蓝牙开关状态
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED) //蓝牙开始搜索
        filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED) //蓝牙搜索结束
        filter.addAction(BluetoothDevice.ACTION_FOUND) //蓝牙发现新设备(未配对的设备)
        filter.addAction(BluetoothDevice.ACTION_PAIRING_REQUEST) //在系统弹出配对框之前(确认/输入配对码)
        filter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED) //设备配对状态改变
        filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED) //最底层连接建立
        filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED) //最底层连接断开
        filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED) //BluetoothAdapter连接状态
        filter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED) //BluetoothHeadset连接状态
        filter.addAction(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED) //BluetoothA2dp连接状态
        cxt.registerReceiver(this, filter)
    }
}